package com.wjpeng.wms.web.controller;

import com.wjpeng.wms.domain.Role;
import com.wjpeng.wms.query.QueryObject;
import com.wjpeng.wms.service.IPermissionService;
import com.wjpeng.wms.service.IRoleService;
import com.wjpeng.wms.service.ISystemmenuService;
import com.wjpeng.wms.util.JSONResult;
import com.wjpeng.wms.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private ISystemmenuService systemmenuService;

    @RequestMapping("list")
    @RequiredPermission("角色列表")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        model.addAttribute("result",roleService.result(qo));
        return "role/list";
    }

    @RequestMapping("input")
    @RequiredPermission("角色编辑")
    public String input(Model model,Long id){
        if(id != null){
            model.addAttribute("entity",roleService.get(id));
        }
        model.addAttribute("permissions",permissionService.list());
        model.addAttribute("menus",systemmenuService.list());

        return "role/input";
    }
    @RequestMapping("delete")
    @ResponseBody
    @RequiredPermission("角色删除")
    public Object delete(Long id){
        roleService.delete(id);
        return new JSONResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiredPermission("角色新增/更新")
    public Object saveOrUpdate(Role role,Long[] permissionIds,Long[] menuIds){
        roleService.saveOrUpdate(role,permissionIds,menuIds);
        return new JSONResult();
    }




}
